package com.label.Web;

import com.label.Model.Common.ResponseBean;
import com.label.Utils.Common.InfoUtil;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
public class PrivewConatroller {

    @Resource
    @Qualifier("oracleJdbcTemplate")
    private JdbcTemplate oracleJdbc;

    @RequestMapping("/getClass")
    public ResponseBean getClass(@RequestParam  String year){
        List<Map<String,Object>> gpaList= oracleJdbc.queryForList("SELECT CLASSID,MAJORID,UNITED,COUNSELORID,AVG(GPA) as avggpa,AVG(GPA11) as avggpa11,AVG(GPA12) as avggpa12,AVG(GPA21) as avggpa21,AVG(GPA22) as avggpa22,AVG(GPA31) as avggpa31,AVG(GPA32) as avggpa32,AVG(GPA41) as avggpa41,AVG(GPA42)  as avggpa42 FROM STUDENTPROFILE WHERE ENROLLYEAR="+year+" GROUP BY CLASSID,MAJORID,UNITED,COUNSELORID ORDER BY CLASSID ");
        String sql="SELECT sp.CLASSID  as classid,SUM(sa.WISDOM) as WISDOM,SUM(sa.ART) as ART,SUM(sa.LABOUR) as LABOUR,SUM(sa.SPORTS) as SPORTS,SUM(sa.VIRTUE) as VIRTUE\n" +
                "FROM\n" +
                "\t((select USERID,CLASSID from STUDENTPROFILE WHERE ENROLLYEAR="+year+")sp LEFT JOIN STUDENTACTIVITY sa on sp.USERID = sa.USERID) GROUP BY sp.CLASSID ORDER BY CLASSID";

        List<Map<String,Object>> fivestarList= oracleJdbc.queryForList(sql);
        Map<String,Map<String,Object>> fivestarmap=fivestarList.stream().collect(Collectors.toMap(p->p.get("CLASSID").toString(),p->p));

        Map<String,Object> resMap=new LinkedHashMap<>();
        String gradeAverageGpa=String.valueOf(Math.round(gpaList.stream().mapToDouble(p-> Double.parseDouble(p.get("AVGGPA").toString())).average().getAsDouble()*100.0)/100.0);
        resMap.put("grade","智能学院"+year+"级");
        resMap.put("grade",year);
        resMap.put("gradeAverageGpa",gradeAverageGpa);

        Map<String,String> seme=new LinkedHashMap<>();
        seme.put("第1学年第1学期","AVGGPA11");
        seme.put("第1学年第2学期","AVGGPA12");
        seme.put("第2学年第1学期","AVGGPA21");
        seme.put("第2学年第2学期","AVGGPA22");
        seme.put("第3学年第1学期","AVGGPA31");
        seme.put("第3学年第2学期","AVGGPA32");
        seme.put("第4学年第1学期","AVGGPA41");
        seme.put("第4学年第2学期","AVGGPA42");

        Map<String,String> edu=new LinkedHashMap<>();

        edu.put("德育分值","VIRTUE");
        edu.put("智育分值","WISDOM");
        edu.put("体育分值","SPORTS");
        edu.put("美育分值","ART");
        edu.put("劳动分值","LABOUR");


        List<Map<String,String>> gradeAvergerGpaSeme=new ArrayList<>();
        seme.forEach((k,v)->{
            Double avg=Math.round(gpaList.stream().mapToDouble(p-> Double.parseDouble(p.get(v).toString())).average().getAsDouble()*100.0)/100.0;
            if(avg>=1){
                Map<String,String> part=new LinkedHashMap<>();
                part.put("name",k);
                part.put("type","年级平均");
                part.put("value",String.valueOf(avg));
                gradeAvergerGpaSeme.add(part);
            }
        });
        resMap.put("gradeAvergerGpaSeme",gradeAvergerGpaSeme);

        List<Map<String,String>> fiveStatAverger=new ArrayList<>();
        edu.forEach((k,v)->{
            Map<String,String> part=new LinkedHashMap<>();
            Double avg=Math.round(fivestarList.stream().filter(p->p.get(v)!=null).mapToDouble(p->Double.parseDouble(p.get(v).toString())).average().getAsDouble()*100.0)/100.0;
            part.put("name",k);
            part.put("type","年级平均");
            part.put("value",String.valueOf(avg));

            fiveStatAverger.add(part);
        });
        resMap.put("fiveStatAverger",fiveStatAverger);


        List<Map<String,Object>> gradeClassData=new ArrayList<>();

        for(Map<String,Object> gpa:gpaList){
            Map<String,Object> gradePartClassData=new LinkedHashMap<>();

            String className=InfoUtil.getClassName(gpa.get("CLASSID").toString());

            List<Map<String,String>> gpadata=new ArrayList<>();
            seme.forEach((k,v)->{
                if(Double.parseDouble(gpa.get(v).toString())>=1){
                    Map<String,String> part=new LinkedHashMap<>();
                    part.put("name",k);
                    part.put("type",className);
                    part.put("value",String.valueOf(Math.round(Double.parseDouble(gpa.get(v).toString())*100.0)/100.0));
                    gpadata.add(part);
                }
            });

            List<Map<String,String>> fivedata=new ArrayList<>();

            edu.forEach((k,v)->{
                Map<String,String> part=new LinkedHashMap<>();

                part.put("name",k);
                part.put("type",className);
                if(fivestarmap.get(gpa.get("CLASSID").toString()).get(v)!=null)
                    part.put("value",String.valueOf(Math.round(Double.parseDouble(fivestarmap.get(gpa.get("CLASSID").toString()).get(v).toString())*100.0)/100.0));

                fivedata.add(part);
            });

            gradePartClassData.put("classId",gpa.get("CLASSID").toString());
            gradePartClassData.put("className",className==null?"无":className);
            gradePartClassData.put("classMajor",InfoUtil.getMajorName(gpa.get("MAJORID").toString()));
            gradePartClassData.put("classUnit",InfoUtil.getUnitName(gpa.get("UNITED").toString()));
            gradePartClassData.put("classCounselor",gpa.get("COUNSELORID")==null?"无":gpa.get("COUNSELORID"));
            gradePartClassData.put("GPAData",gpadata);
            gradePartClassData.put("fiveStarData",fivedata);
            gradePartClassData.put("gradeAverageGpa",gradeAverageGpa);
            gradePartClassData.put("classAvgGpa",gpa.get("AVGGPA")==null?"无":String.valueOf(Math.round(Double.parseDouble(gpa.get("AVGGPA").toString())*100.0)/100.0));

            gradeClassData.add(gradePartClassData);

        }

        resMap.put("gradeClassData",gradeClassData);

        return new ResponseBean(200,"成功",resMap);
    }

}
